package com.randi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.randi.common.annotation.LogAnnotation;
import com.randi.common.core.domain.Result;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;
import com.randi.system.domain.dto.NoticeBody;
import com.randi.system.domain.entity.sysNotice.SysNotice;
import com.randi.system.domain.entity.sysNotice.SysNoticeRead;
import com.randi.system.domain.params.SysNoticeParam;
import com.randi.system.service.sysNotice.ISysNoticeReadService;
import com.randi.system.service.sysNotice.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ziye
 */
@RestController
@RequestMapping("system/notice")
@Api(tags = "公告管理")
public class SysNoticeController {

    @Resource
    private ISysNoticeService iSysNoticeService;

    @Resource
    private ISysNoticeReadService iSysNoticeReadService;

    /**
     * 分页获取列表
     *
     * @param sysNoticeParam 检索参数
     * @return Result
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取列表", notes = "")
    @PreAuthorize("@auth.hasPermission('system:notice:list')")
    public Result selectList(@RequestBody SysNoticeParam sysNoticeParam) {
        PageInfo<SysNotice> sysNoticeList = iSysNoticeService.selectPageList(sysNoticeParam);
        return Result.success().putPage(sysNoticeList);
    }

    /**
     * 查看详情
     *
     * @param noticeId noticeId
     * @return Result
     */
    @GetMapping("/detail/{noticeId}")
    @ApiOperation(value = "查看详情", notes = "")
    @PreAuthorize("@auth.hasPermission('system:notice:list')")
    public Result selectById(@PathVariable Long noticeId) {
        SysNotice sysNotice = iSysNoticeService.selectSysNoticeById(noticeId);
        return Result.success().put(sysNotice);
    }

    /**
     * 插入数据
     *
     * @param noticeBody 业务信息
     * @return 结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "插入数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:notice:add')")
    @LogAnnotation(module = "公告管理", value = "插入数据", businessType = BusinessType.INSERT)
    public Result insert(@RequestBody NoticeBody noticeBody) {
        int status = iSysNoticeService.insertSysNotice(noticeBody);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_INSERT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_INPUT_SUCCESS);
    }

    /**
     * 更新数据
     *
     * @param sysNotice 业务信息
     * @return 结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:notice:edit')")
    @LogAnnotation(module = "公告管理", value = "更新数据", businessType = BusinessType.UPDATE)
    public Result update(@RequestBody SysNotice sysNotice) {
        int status = iSysNoticeService.updateSysNotice(sysNotice);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_INSERT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_EDITOR_SUCCESS);
    }

    /**
     * 删除数据
     *
     * @param noticeIds noticeIds
     * @return 结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除", notes = "")
    @PreAuthorize("@auth.hasPermission('system:notice:delete')")
    @LogAnnotation(module = "公告管理", value = "删除数据", businessType = BusinessType.DELETE)
    public Result deleteBatch(@RequestBody List<Long> noticeIds) {
        int status = iSysNoticeService.deleteBatchSysNotice(noticeIds);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }

    /**
     * 公告阅读
     *
     * @return 推送结果
     */
    @GetMapping("/read")
    @ApiOperation(value = "公告阅读", notes = "")
    @PreAuthorize("@auth.hasPermission('system:notice:push')")
    public Result push(@RequestParam Long noticeId, @RequestParam Long userId) {
        int status = iSysNoticeReadService.insertSysNoticeRead(new SysNoticeRead(noticeId, userId));
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_INSERT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_INPUT_SUCCESS).put(status);
    }

}